<script type="text/javascript">
	function doSearchRoomConfig(value) {
		
	}
	
	//表格查询  
	function reloadRoomConfig() {
		var params = roomConfig_list.datagrid('options').queryParams; //先取得 datagrid 的查询参数  
		printLog(params);
		roomConfig_list.datagrid('reload'); //设置好查询参数 reload 一下就可以了  
		roomConfig_list.datagrid('unselectAll');
		roomConfig_list.datagrid('uncheckAll');
		
		var params = $('#deviceLink_selectlist').datagrid('options').queryParams;
		params['roomType'] = '';
		deviceLink_selectlist.datagrid('reload'); //设置好查询参数 reload 一下就可以了  
		deviceLink_selectlist.datagrid('unselectAll');
		deviceLink_selectlist.datagrid('uncheckAll');
		
		$('#roomConfigForm').form('clear');
	}


	function addRoomConfig() {
		reloadRoomConfig();
	}
	
	function deleteRoomConfig() {
		var rows = roomConfig_list.datagrid('getChecked');
	    if (rows&&rows.length>0){
	        $.messager.confirm('询问','你确认要删除吗?',function(r){
	            if (r){
	            	var ids = "";
	            	for(var i=0; i<rows.length; i++){
	            		ids += rows[i].id+",";
	            	}
	                $.post('roomConfig.do?deleteRoomConfigItems',{ids:ids},function(result){
	                    if (result.success){
	                    	reloadRoomConfig();    
	                    }
	                    showMsg(result.msg);
	                },'json');
	            }
	        });
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}

	function editRoomConfig() {
		var rows = roomConfig_list.datagrid('getChecked');
		printLog(rows);
		if (rows&&rows.length>0) {
			if(rows.length>1) {
				showMsg('请选择一条操作记录.');
				return;
			}
			createFromWindowOptions({
				title:"编辑信息",
				maximizable:true,
				href: "roomConfig.do?editRoomConfig&roomConfigId=" + rows[0].id,
				callback:function(data) {
					addDeviceLinkInfo();
				},
				enableSaveButton:true
			});
		} else {
			showMsg('请选择操作记录.');
		}

	}
	
	
	
	var roomConfigType="";
	function onClickDeviceLink(rowIndex, rowData) {
		printLog(rowData);
		$('#roomConfigForm').form('load', rowData);
		roomConfigType = rowData.roomType;
		reloadConfigDevice();
	}
	
	function reloadConfigDevice() {
		//先取得 datagrid 的查询参数  
		var params = $('#deviceLink_selectlist').datagrid('options').queryParams;
		params['roomType'] = roomConfigType;
		//设置好查询参数 reload 一下就可以了 
		$('#deviceLink_selectlist').datagrid('reload');  
		deviceLink_selectlist.datagrid('unselectAll');
		deviceLink_selectlist.datagrid('uncheckAll');
	}
	
	function updateOrSaveRoomConfig() {
		var validate = $('#roomConfigForm').form("validate");
        if (validate) {
        	var rows = $('#deviceLink_selectlist').datagrid('getRows');
    		var roomConfigInfo = {
    			id:$('#roomConfigId').val(),
    			roomType:$('#roomConfigRoomType').combobox('getText'),
    			configInfo:$('#configInfo').val(),
    			deviceLinks:rows
    		};
    		printLog({data:JSON.stringify(roomConfigInfo)});
    		$.post("roomConfig.do?newSaveRoomConfigInfos", {data:JSON.stringify(roomConfigInfo)}, function(result) {
    	          if(result.success){
    		       	  showMsg('保存成功.');
    		       	  reloadRoomConfig();
    	          }else {
    	       	  	  showMsg('保存失败.');
    	          }
    	  	}, "JSON");
        }
		
	}
	
	$.extend($.fn.validatebox.defaults.rules,{
// 		remote['roomConfig.do?checkConfigIsExit&id=${(roomConfigItem.id)!}','roomType']" invalidMessage="请勿重复配置相同酒店类型"
	   checkRoomConfig:{
	   validator:function(value,param){
			printLog(value);
			printLog(param);
			var roomType = $('#roomConfigRoomType').combobox('getText');
			var roomConfigId = $('#roomConfigId').val();
			var rs = true;
			if(roomType) {
				
				jmaxSyncAjax('roomConfig.do?checkConfigIsExit',{id:roomConfigId,roomType:roomType},function(data) {
					printLog('rs1='+rs);
					rs = data;
				});
			}
			printLog('rs2='+rs);
			return rs;
	    },
	    message:'请勿重复配置相同房间类型'
	   }
	  });
	
	//新增设备
	function roomConfigAddDeviceLink() {
		createFromWindowOptions({
			title:"新增信息",
			maximizable:true,
			width:1100,
			height:300,
			href:"deviceLink.do?addDeviceLinkInConfig",
			callback:function(data) {
				addDeviceLinkInfo();
			},
			enableSaveButton:true
		});
	}
	
	function roomConfigDeleteDeviceLink() {
		var rows = deviceLink_selectlist.datagrid('getChecked');
	    if (rows&&rows.length>0){
	        $.messager.confirm('询问','你确认要删除吗?',function(r){
	        	if (r){
	            	var ids = "";
	            	for(var i=0; i<rows.length; i++){
	            		ids += rows[i].id+","; 
   	    	        	var rowIndex = deviceLink_selectlist.datagrid('getRowIndex', rows[i]);
   	    	        	deviceLink_selectlist.datagrid('deleteRow', rowIndex);
	            	}
	                $.post('deviceLink.do?deleteDeviceLinkItems',{ids:ids},function(result){
	                    if (result.success){
	                    	deviceLink_selectlist.datagrid('unselectAll');
	    	        		deviceLink_selectlist.datagrid('uncheckAll');
	                    }
// 	                    showMsg(result.msg);
	                },'json');
	            }
	        });
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}
	
	function roomConfigEditDeviceLink() {
		var rows = deviceLink_selectlist.datagrid('getChecked');
		if (rows&&rows.length>0) {
			if(rows.length>1) {
				showMsg('请选择一条操作记录.');
				return;
			}
			createFromWindowOptions({
				title:"编辑信息",
				width:1100,
				height:300,
				maximizable:true,
				href: "deviceLink.do?addDeviceLinkInConfig&rowIndex="+deviceLink_selectlist.datagrid('getRowIndex',rows[0])+"&deviceLinkId=" + rows[0].id,
				callback:function(data) {
					addDeviceLinkInfo();
				},
				enableSaveButton:true
			});
		} else {
			showMsg('请选择操作记录.');
		}

	}
	
</script>

<div class="easyui-layout" data-options="fit:true">
	<div data-options="region: 'west',border:false, split: true," style="width: 450px;">
				<@eu.datagrid id="roomConfig_list" toolbar="#roomConfiglist_search" idField="id" 
					 url="roomConfig.do?loadList" rownumbers="true" onClickRow="onClickDeviceLink(rowIndex, rowData)"
					 item="com.xdtech.hotel.vo.RoomConfigItem" pagination="false"/>
				<div id="roomConfiglist_search">
					<div id="roomConfig_toolbar" style="height: auto">
					    <@eu.linkbutton showName="新增" shiro="hotel:roomConfig:add" iconCls="icon-add" onclick="addRoomConfig()"/>
						<@eu.linkbutton showName="删除" shiro="hotel:roomConfig:delete" iconCls="icon-remove" onclick="deleteRoomConfig()"/>
						<@eu.linkbutton showName="刷新" shiro="hotel:roomConfig:refresh" iconCls="icon-reload" onclick="reloadRoomConfig()"/>
					</div>
				</div>
	</div>
	<div data-options="region:'center',border:false">
		 <div class="easyui-layout" data-options="fit:true">
       		<div data-options="region:'north',border:false" style="height:30px;overflow: hidden;background:#F4F4F4">
			    <form id="roomConfigForm" action="">
			    	<input type="hidden" id="roomConfigId" name="id"/>
			    	<table>
			    		<tr>
			    			<td class="tbLabel">房间类型:</td>
			    			<td>
			    				<@eu.comboBox id="roomConfigRoomType" url="common.do?loadComboBox&key=ROOM_TYPE&value=${(roomConfigItem.roomType)!}" name="roomType" required="true" validType="checkRoomConfig[]"/>
			    				<input name="configInfo" type="hidden" />
			    			</td>
<!-- 			    			<td class="tbLabel"> -->
<!-- 			    				配置信息: -->
<!-- 			    			</td>  -->
<!-- 			    			<td> -->
<!-- 			    				<textarea id="configInfo" style="height: 80px;width:400px" rows="6"  name="configInfo" class="easyui-validatebox" readonly="readonly" >${(roomConfigItem.configInfo)!}</textarea> -->
<!-- 			    			</td> -->
			    		</tr>
			    	</table>
			    </form>
       		</div>
       		<div data-options="region:'center',border:false">
			       <@eu.datagrid id="deviceLink_selectlist" toolbar="#deviceLinklist_search" title="已配置设备" idField="id" pagination="false"
				 url="roomConfig.do?loadRoomConfigSelectDeviceLinks" rownumbers="true"
				 item="com.xdtech.hotel.vo.DeviceLinkItem"/>
		
				<div id="deviceLinklist_search">
					<div id="deviceLink_toolbar" style="height: auto">
						<@eu.linkbutton showName="新增" shiro="hotel:deviceLink:add" iconCls="icon-add" onclick="roomConfigAddDeviceLink()"/>
						<@eu.linkbutton showName="删除" shiro="hotel:deviceLink:delete" iconCls="icon-remove" onclick="roomConfigDeleteDeviceLink()"/>
						<@eu.linkbutton showName="编辑" shiro="hotel:deviceLink:edit" iconCls="icon-edit" onclick="roomConfigEditDeviceLink()"/>
<!-- 						<@eu.linkbutton showName="刷新" shiro="hotel:deviceLink:refresh" iconCls="icon-reload" onclick="reloadDeviceLink()"/> -->
					</div>
				</div>
				
       		</div>
       		<div data-options="region:'south',border:false">
				<div style="padding:3px 0;border-top:1px solid #95B8E7;text-align: center;">
					<@eu.linkbutton showName="更新或保存" shiro="hotel:roomConfig:updateOrSave" iconCls="icon-edit" style="border:1px solid #bbb;background: #eaf2ff;" onclick="updateOrSaveRoomConfig()"/>
			    </div>
			</div>
       </div>
		
		
	</div>
</div>